package com.kawins.util.encrypt;


import java.io.IOException;
import java.io.UnsupportedEncodingException;

/**
 * @author rankai
 *         createTime 2018-01-2018/1/2 13:08
 */
public class Base64 {

    /**
     * 加密
     */
    public static String encryptBase64(String str) {
        return java.util.Base64.getEncoder().encodeToString(str.getBytes());
    }

    /**
     * 加密
     */
    public static String encryptBase64(String str, String charSet) {
        try {
            return java.util.Base64.getEncoder().encodeToString(str.getBytes(charSet));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 解密
     *
     * @param str
     * @return
     */
    public static String decodeBase64(String str) {
        return new String(java.util.Base64.getDecoder().decode(str));
    }

    /**
     * 解密
     *
     * @param str
     * @return
     */
    public static String decodeBase64(String str, String charSet) {
        try {
            return new String(java.util.Base64.getDecoder().decode(str), charSet);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) throws Exception {
        String str = "kawins";
        String s = Base64.encryptBase64(str);
        System.out.println(s);
        System.out.println(Base64.decodeBase64(s));
    }
}
